package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.domain.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import java.time.LocalDateTime;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;

@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao,ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车 数据
     * @param shoppingCart
     * @return
     */
    @Override
    @Transactional   //开启事务
    public ShoppingCart add(ShoppingCart shoppingCart) {

        // 设置是哪一个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();  //获取当前用户id
        shoppingCart.setUserId(currentId);

        // 查询当前菜品或套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();  //获取菜品id，判断是菜品还是套餐

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);    // user_id  = id

        if(dishId != null){
            //添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        // SQL : select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart serviceOne = shoppingCartService.getOne(queryWrapper);

        // 如果已经存在就在原来的基础上数量加一
        if(serviceOne != null){
            Integer number = serviceOne.getNumber();
            serviceOne.setNumber(number + 1);
            //SQL : update shopping_cart set number = 1 where id = id
            shoppingCartService.updateById(serviceOne);

        }else {
            // 如果不存在就添加到购物车，默认数量为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now()); //创建时间

            // SQL: insert into shopping_cart values(?,?,?,?,?,?,?,?,?,?)
            shoppingCartService.save(shoppingCart);

            //将当前购物车数据 赋值给 serviceOne 对象
            serviceOne = shoppingCart;
        }

        return serviceOne;
    }


    /**
     * 修改购物车  菜品或套餐 数量
     * @param shoppingCart
     * @return
     */
    @Override
    @Transactional     //开启事务
    public ShoppingCart delete(ShoppingCart shoppingCart) {
        // 获取菜品id
        Long dishId = shoppingCart.getDishId();  //用来判断是菜品 还是套餐

        //构造条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        //判断 菜品 还是 套餐
        if(dishId != null) {
            //是菜品
            queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        }else {
            //是套餐
            queryWrapper.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        // SQL: select * from shopping_cart where userid_id =? and dish_id/setmeal_id = ?
        ShoppingCart serviceOne = shoppingCartService.getOne(queryWrapper);   // 查询到的购物车对象(某一条记录)
        Integer index = serviceOne.getNumber(); //当前 这条记录 数量

        //判断是否存在
        if(serviceOne != null && index >1){
            //存在
            Integer number = serviceOne.getNumber();
            serviceOne.setNumber(number - 1);         // 数量减 1
            shoppingCartService.updateById(serviceOne);

            return serviceOne;
        }else {
            // 不存在
            LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            //如果是菜品
            lambdaQueryWrapper.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
            //如果是套餐
            lambdaQueryWrapper.eq(shoppingCart.getSetmealId() !=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

            // SQL: delete from shopping_cart where user_id = ?  and dish_id/setmeal_id =?
            shoppingCartService.remove(lambdaQueryWrapper);

            serviceOne.setNumber(0);
            return serviceOne;
        }
    }


    /**
     * 清空购物车数据
     * @return
     */
    @Transactional // 开启事务
    public R<String> clean(){

        //构建条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        //DELETE FROM shopping_cart WHERE (user_id = ?)
        shoppingCartService.remove(queryWrapper);

        return R.success("删除成功");
    }


}
